package com.sparrow.event.receiver.impl;

import com.sparrow.event.model.Event;
import com.sparrow.common.event.IEventInvoker;
import com.sparrow.event.receiver.IEventReceiver;

/**
 * 事件接受者
 */
public class EventReceiver implements IEventReceiver {
    /**
     * 使用接口封装 不同的invoker，invoker 可以是反射invoker, 也可以是bytebuddy生成的协议invoker
     */
    private final IEventInvoker invoker;

    private final int order;

    public EventReceiver(IEventInvoker invoker, int order) {
        this.invoker = invoker;
        this.order = order;
    }

    @Override
    public void receive(Event event) {
        try {
            Object[] args = event.getArgs();
            invoker.invoke(args);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public int getOrder() {
        return order;
    }

    @Override
    public IEventInvoker getInvoker() {
        return invoker;
    }

}
